package com.itheima.info.manager.controller;

import com.itheima.info.manager.domain.Student;
import com.itheima.info.manager.service.StudentService;

import java.util.Scanner;

public class OtherStudentController extends BaseStudentController{
    Scanner scanner = new Scanner(System.in);
    StudentService studentService = new StudentService();
//    StudentController studentController=new StudentController();

    public void start() {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudentById();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理系统，再见！");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误，请重新输入");
            }

        }
    }

    private void updateStudent() {
        String updateId;
        if (null == studentService.findAllStudent()) {
            System.out.println("无数据，无法更新，添加后重试！");
            return;
        }

        updateId = inputStudentId();
        Student newStudent = inputStudentInfo(updateId);
        studentService.updateStudent(updateId, newStudent);
        System.out.println("修改成功！");
        /*while (true) {
            System.out.print("请输入要修改的学号：");
            updateId = scanner.next();
            boolean exists = studentService.isExists(updateId);
            if (!exists) {
                System.out.println("不存在该学号，无法修改");
            } else {
                break;
            }
        }
         */
        /*System.out.print("请输入学生姓名：");
        String s_name = scanner.next();
        System.out.print("请输入学生年龄：");
        String s_age = scanner.next();
        System.out.print("请输入学生地址：");
        String s_address = scanner.next();

        newStudent.setId(updateId);
        newStudent.setName(s_name);
        newStudent.setAge(s_age);
        newStudent.setAddress(s_address);*/

    }

    private void deleteStudentById() {
        String deleteId;
        if (null == studentService.findAllStudent()) {
            System.out.println("无数据，无法删除，添加后重试！");
            return;
        }

        deleteId = inputStudentId();
        studentService.deleteStudentById(deleteId);
        System.out.println("删除成功！");
        /*while (true) {
            System.out.print("请输入要删除的学号：");
            deleteId = scanner.next();
            boolean exists = studentService.isExists(deleteId);
            if (!exists) {
                System.out.println("不存在该学号，无法删除");
            } else {
                break;
            }
        }
         */

    }


    private void findAllStudent() {
        Student[] students = studentService.findAllStudent();
        if (null == students) {
            System.out.println("无数据，请添加后重新查询！");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t\t地址");
        for (int i = 0; i < students.length; i++) {
            if (null != students[i]) {
                System.out.println(students[i].getId() + "\t" + students[i].getName()
                        + "\t\t" + students[i].getAge() + "\t\t" + students[i].getAddress());
            }
        }
    }

    private void addStudent() {
//        String s_id = inputStudentId();
        String s_id;
        while (true) {
            System.out.print("请输入学生学号：");
            s_id = scanner.next();
            boolean exists = studentService.isExists(s_id);
            if (exists) {
                System.out.println("学号已被占用，请重新输入");
            } else {
                break;
            }
        }
        Student student = inputStudentInfo(s_id);

        boolean add_result = studentService.addStudent(student);

        if (add_result) System.out.println("添加成功！");
        else System.out.println("添加失败！");

    }

    private Student inputStudentInfo(String s_id) {
        System.out.print("请输入学生姓名：");
        String s_name = scanner.next();
        System.out.print("请输入学生年龄：");
        String s_age = scanner.next();
        System.out.print("请输入学生地址：");
        String s_address = scanner.next();

        Student student = new Student(s_id,s_name,s_age,s_address);
        return student;
    }

    private String inputStudentId() {
        String s_id;
        while (true) {
            System.out.print("请输入学生学号：");
            s_id = scanner.next();
            boolean exists = studentService.isExists(s_id);
            if (!exists) {
                System.out.println("学号不存在，请重新输入");
            } else {
                break;
            }
        }
        return s_id;
    }


}
